/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-23 19:51:46
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.risk.service.impl;

import com.google.common.collect.Lists;
import com.joysuch.wwyt.api.dataaccess.DataAccessService;
import com.joysuch.wwyt.api.dataaccess.bean.ThreeCardInBean;
import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BaseAttachmentRepositoryService;
import com.joysuch.wwyt.core.service.BaseLoginAccountService;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.risk.bean.RiskPromiseCardListBean;
import com.joysuch.wwyt.risk.entity.RiskPromiseCard;
import com.joysuch.wwyt.risk.entity.vo.RiskPromiseCardVo;
import com.joysuch.wwyt.risk.repository.RiskPromiseCardDao;
import com.joysuch.wwyt.risk.service.RiskDistrictService;
import com.joysuch.wwyt.risk.service.RiskPromiseCardService;
import com.joysuch.wwyt.util.ImageRefUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class RiskPromiseCardServiceImpl implements RiskPromiseCardService {

    @Autowired
    private RiskPromiseCardDao dao;
    @Autowired
    private BaseLoginAccountService loginAccountService;
    @Autowired
    private RiskDistrictService districtService;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private DataAccessService dataAccessService;
    @Autowired
    private BpJobDao bpJobDao;
    @Autowired
    private BaseAttachmentRepositoryService attachmentService;

    @Override
    public void add(RiskPromiseCardVo entityVo) throws Exception {
        entityVo.setCode(bizCodeGeneratorFacade.chengNuoKa());
        if (entityVo.getAttachment() != null) {
            entityVo.setAttachmentId(entityVo.getAttachment().getId());
        }
        RiskPromiseCard save = new RiskPromiseCard();
        BeanUtils.copyProperties(entityVo,save);
        save = dao.save(save);
        //调数据接入接口
        //判断有没有照片
        //<p>bbbb<img src="/image/view?id=446" style="max-width: 100%;"></p>
        String content = save.getContent();
        if (content != null && content.contains("img")) {
            ThreeCardInBean bean = new ThreeCardInBean();
            bean.setPicId(ImageRefUtil.getImageId(content));
            bean.setCode(save.getCode());
            bean.setRiskDistrictId(save.getRiskDistrictId());
            bean.setPicName("安全生产承诺卡");
            dataAccessService.threeCard(Lists.newArrayList(bean));
        }
    }


    @Override
    public void update(RiskPromiseCardVo entityVo) throws Exception {
        RiskPromiseCard content = dao.findById(entityVo.getId()).get();
        if (content != null) {
            content.setContent(entityVo.getContent());
            content.setDescription(entityVo.getDescription());
            content.setRiskDistrictId(entityVo.getRiskDistrictId());
            content.setName(entityVo.getName());
            content.setJobId(entityVo.getJobId());
            content.setCertNumber(entityVo.getCertNumber());
            content.setManagerUserId(entityVo.getManagerUserId());
            content.setManagerCertNumber(entityVo.getManagerCertNumber());
            content.setPromiseContent(entityVo.getPromiseContent());
            content.setPromiseDate(entityVo.getPromiseDate());
            content.setPromiseUserId(entityVo.getPromiseUserId());
            if (entityVo.getAttachment() != null) {
                content.setAttachmentId(entityVo.getAttachment().getId());
            }else {
                content.setAttachmentId(null);
            }
            dao.save(content);
        }
    }

    @Override
    public Page<RiskPromiseCardListBean> findByPage(Pageable page) {
        Page<RiskPromiseCard> all = dao.findAll(page);
        List<RiskPromiseCardListBean> list = new ArrayList<RiskPromiseCardListBean>(all.getContent().size());
        for (RiskPromiseCard c : all) {
            list.add(convert(c));
        }
        return new PageImpl<RiskPromiseCardListBean>(list, all.getPageable(), all.getTotalElements());
    }

    private RiskPromiseCardListBean convert(RiskPromiseCard c) {
        RiskPromiseCardListBean bean = new RiskPromiseCardListBean();
        bean.setCode(c.getCode());
        bean.setId(c.getId());
        bean.setCreateTime(SimpleDateFormatCache.getYmdhms().format(c.getCreateTime()));
        bean.setCreator(baseUserDao.getBaseUserNameById(c.getCreateBy()));
        bean.setRiskDistrictId(c.getRiskDistrictId());
        bean.setRiskDistrictName(districtService.findDistrictNameById(c.getRiskDistrictId()));
        bean.setContent(c.getContent());
        bean.setDescription(c.getDescription());
        Long jobId = c.getJobId();
        bean.setJobId(jobId);
        bean.setJobName(jobId == null ? null : bpJobDao.getNameById(jobId));
        bean.setName(c.getName());
        bean.setCertNumber(c.getCertNumber());
        bean.setManagerUserId(c.getManagerUserId());
        Long managerUserId = c.getManagerUserId();
        bean.setManagerUserName(managerUserId == null ? null : baseUserDao.getRealNameById(managerUserId));
        bean.setManagerCertNumber(c.getManagerCertNumber());
        bean.setPromiseContent(c.getPromiseContent());
        bean.setPromiseDate(c.getPromiseDate());
        Long promiseUserId = c.getPromiseUserId();
        bean.setPromiseUserId(promiseUserId);
        bean.setPromiseUserName(promiseUserId == null ? null : baseUserDao.getRealNameById(promiseUserId));
        if (c.getAttachmentId() != null) {
            BaseAttachmentBean attachment = attachmentService.findByIdIntoBean(c.getAttachmentId());
            bean.setAttachment(attachment);
        }
        return bean;
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskPromiseCard findById(Long id) {
        return dao.getOne(id);
    }

    @Override
    public RiskPromiseCardListBean findBeanById(Long id) {
        RiskPromiseCard card = findById(id);
        return card == null ? null : convert(card);
    }

}
